@layout SettingsLayout
@page "/admin/settings"
@inject HttpClient _http
@inject IToaster _toaster
@inject IStringLocalizer<Resource> _localizer

<PageTitleComponent Title="@_localizer["basic-settings"]" />

<h1 class="section-title">@_localizer["basic-settings"]</h1>
<div class="section-content -half">
  @if (Blog != null)
  {
    <EditForm model="@Blog" OnValidSubmit="Save">
      <DataAnnotationsValidator />
      <ValidationSummary />
      <div class="form-item">
        <label class="form-label">@_localizer["default-cover"]</label>
        <button class="settings-cover" type="button" onclick="">
          <img class="settings-cover-img" id="defaultCover" src="@BlogifierSharedConstant.DefaultCover" alt="@_localizer["default-cover"]">
        </button>
      </div>
      <div class="form-item">
        <label class="form-label">@_localizer["title"]</label>
        <input type="text" class="form-control" @bind="Blog.Title" name="title" />
      </div>
      <div class="form-item">
        <label class="form-label">@_localizer["description"]</label>
        <textarea class="form-control" cols="30" rows="5" @bind="Blog.Description" name="description"></textarea>
      </div>
      <div class="form-item">
        <button type="submit" class="btn btn-blogifier px-5">@_localizer["save"]</button>
      </div>
    </EditForm>
  }
</div>

@code {
  protected BlogEitorDto? Blog { get; set; }

  protected override async Task OnInitializedAsync()
  {
    Blog = await _http.GetFromJsonAsync<BlogEitorDto>("api/blog");
  }

  protected async Task Save()
  {
    Toast(await _http.PutAsJsonAsync<BlogEitorDto>("api/blog", Blog!));
  }

  protected void Toast(HttpResponseMessage msg)
  {
    if (msg.IsSuccessStatusCode)
      _toaster.Success(_localizer["completed"]);
    else
      _toaster.Error(_localizer["generic-error"]);
  }
}
